﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Configuration;
using System.Data;
namespace Piao
{
    class Configurations
    {
        private static Configurations internalInstance = null;
        public Configurations()
        { }

        public static Configurations Instance
        {
            get
            {
                if (internalInstance == null)
                    internalInstance = new Configurations();
                return internalInstance;
            }
        }


        static Configurations()
        {
            string file = System.Reflection.Assembly.GetExecutingAssembly().Location;

            if (System.ComponentModel.LicenseManager.UsageMode == System.ComponentModel.LicenseUsageMode.Designtime)
            {
                file = "app.config";
            }

            config = ConfigurationManager.OpenExeConfiguration(file);

            appSettings = config.AppSettings;
            userSection = (ClientSettingsSection)config.GetSection(userSectionName);

        }

        const string appSectionName = "appSettings";
        const string userSectionName = "userSettings/Piao.Properties.Settings";
        static Configuration config = null;

        static ClientSettingsSection userSection;
        static AppSettingsSection appSettings;

        public void Save()
        {
            try
            {
                config.Save(ConfigurationSaveMode.Minimal, true);
                ConfigurationManager.RefreshSection(appSectionName);
                ConfigurationManager.RefreshSection(userSectionName);


            }
            catch (System.Configuration.ConfigurationErrorsException)
            {

            }

        }

        private string GetAppSetting(string key)
        {
            if (appSettings.Settings.AllKeys.Contains(key))
                return appSettings.Settings[key].Value;
            else
                return string.Empty;
        }
        private void UpdateAppSetting(string key, string value)
        {
            if (!appSettings.Settings.AllKeys.Contains(key))
                appSettings.Settings.Add(key, value);
            else
                appSettings.Settings[key].Value = value;
        }


        #region app setting
        public string txt_userFromStation
        {
            get
            {
                return GetAppSetting("txt_userFromStation");
            }
            set
            {
                UpdateAppSetting("txt_userFromStation", value);
            }
        }

        public string txt_userToStation
        {
            get
            {
                return GetAppSetting("txt_userToStation");
            }
            set
            {
                UpdateAppSetting("txt_userToStation", value);
            }
        }

        public string txt_userTrains
        {
            get
            {
                return GetAppSetting("txt_userTrains");
            }
            set
            {
                UpdateAppSetting("txt_userTrains", value);
            }
        }

        public string chk_userSeatTypes
        {
            get
            {
                return GetAppSetting("chk_userSeatTypes");
            }
            set
            {
                UpdateAppSetting("chk_userSeatTypes", value);
            }
        }

        public int HttpTimeout
        {
            get
            {
                var v = GetAppSetting("timeout");
                int timeout = 60;
                int.TryParse(v, out timeout);

                timeout = timeout < 10 ? 10 : timeout;
                timeout = timeout > 60 ? 60 : timeout;
                return timeout * 1000; //second to millisecond


            }
        }

        public int DaysAhead
        {
            get
            {
                int d = 19;
                var v = GetAppSetting("daysAhead");

                int.TryParse(v, out d);

                return d;
            }
        }

        #endregion


        #region user section settings
        private T GetUserSectionValue<T>(string key) where T : class
        {
            try
            {
                var element = userSection.Settings.Get(key);
                if (element == null)
                    return default(T);
                return element.Value.ValueXml.InnerText.Trim() as T;
            }
            catch (Exception)
            {
                return default(T);
            }
        }

        private void SetUserSectionValue(string key, string value)
        {
            SetUserSectionValue(key, value, SettingsSerializeAs.String);
        }

        private void SetUserSectionValue(string key, string value, SettingsSerializeAs ty)
        {
            if (string.IsNullOrEmpty(value))
                return;
            var element = userSection.Settings.Get(key);
             
            if (element == null)
            {
                element = new SettingElement(key, ty);
                userSection.Settings.Add(element);
            }
            else
                element.Value.ValueXml.InnerXml = value;
        }

        public System.Collections.Specialized.StringCollection ProxyList
        {
            get
            {
                try
                {
                    var collection = new System.Collections.Specialized.StringCollection();

                    string text = GetUserSectionValue<string>("WebProxy");
                    using (System.IO.StringReader sr = new System.IO.StringReader(text))
                    {
                        while (sr.Peek() > 0)
                        {
                            string line = sr.ReadLine().Trim();
                            if (!string.IsNullOrEmpty(line))
                                collection.Add(line);
                        }
                        return collection;
                    }
                }
                catch (Exception)
                {
                }
                return null;
            }
        }

        public System.Collections.Specialized.StringCollection PassengerList
        {
            get
            {
                try
                {
                    var collection = new System.Collections.Specialized.StringCollection();

                    string text = GetUserSectionValue<string>("Passengers");
                    using (System.IO.StringReader sr = new System.IO.StringReader(text))
                    {
                        while (sr.Peek() > 0)
                        {
                            string line = sr.ReadLine().Trim();
                            if (!string.IsNullOrEmpty(line))
                                collection.Add(line);
                        }
                        return collection;
                    }
                }
                catch (Exception)
                {
                }
                return null;
            }
            set
            {
                try
                {
                    var collection = value as System.Collections.Specialized.StringCollection;
                    if (collection == null)
                        return;
                    //偷个懒吧
                    StringBuilder sb = new StringBuilder(
            @"<ArrayOfString xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
              xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
              ");
                    foreach (string s in collection)
                    {
                        sb.AppendLine(string.Format("<string>{0}</string>", s));
                    }
                    sb.AppendLine("</ArrayOfString>");

                    SetUserSectionValue("Passengers", sb.ToString(), SettingsSerializeAs.Xml);
                }
                catch (Exception)
                {
                }
            }
        }

        public string WebUserName
        {
            get
            {
                return GetUserSectionValue<string>("WebUserName");
            }
            set
            {
                SetUserSectionValue("WebUserName", value);
            }
        }

        public string WebUserPassword
        {
            get
            {
                return GetUserSectionValue<string>("WebUserPassword");
            }
            set
            {
                SetUserSectionValue("WebUserPassword", value);
            }
        }

        public string UserSeatName
        {
            get
            {
                return GetUserSectionValue<string>("UserSeatName");
            }
            set
            {
                SetUserSectionValue("UserSeatName", value);
            }
        }

        /// <summary>
        /// 身份证
        /// </summary>
        public string UserserSeatIdType
        {
            get
            {
                return GetUserSectionValue<string>("UserserSeatIdType");

            }
            set
            {
                SetUserSectionValue("UserserSeatIdType", value);

            }
        }

        /// <summary>
        /// 身份证
        /// </summary>
        public string UserIdNumber
        {
            get
            {
                return GetUserSectionValue<string>("UserIdNumber");

            }
            set
            {
                SetUserSectionValue("UserIdNumber", value);

            }
        }

        public string UserSeatTel
        {
            get
            {
                return GetUserSectionValue<string>("UserSeatTel");

            }
            set
            {
                SetUserSectionValue("UserSeatTel", value);

            }
        }

        #endregion



    }



}
